package com.wontube.smr.bo.sites.fs.api;

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.wontube.smr.bo.sites.fs.conf.FourShared;



/**
 * @author yuechao
 * 
 */
public final class Mp3FileInfoAPI extends FSAPI {
	private int bitrate;
	private int duration;
	
	public int getBitrate() {
		return this.bitrate;
	}

	public void setBitrate(int bitrate) {
		this.bitrate = bitrate;
	}

	public int getDuration() {
		return this.duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public Mp3FileInfoAPI getMp3FileInfo(String url) {
		String method = "getMp3FileInfo";
		String[] args = new String[] {
				FourShared.fs.getUsername(), 
				FourShared.fs.getPassword(), 
				url
		};

	    Document doc = super.invoke(method, args);
	    if (doc == null) {
	    	return this;
	    }
	    
		NodeList list = doc.getElementsByTagName("return");
		Node node = list.item(0);
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			Element element = (Element)node;
			node = element.getElementsByTagName("bitrate").item(0).getFirstChild();
			this.bitrate = Integer.parseInt(node.getNodeValue());
			node = element.getElementsByTagName("length").item(0).getFirstChild();
			this.duration = Integer.parseInt(node.getNodeValue());
		}
		
		return this;
	}
}
